// build : 20240910
import { udpClass } from './libvbar-drv-dxudp.so'
import dxCommon from './dxCommon.js'
const udpObj = new udpClass();
const udp = {}

/**
 * UDP creation
 * @param {object} config UDP related parameters, required.
 * 		@param {string} config.group_ip UDP multicast address
 *      @param {number} config.group_port upd multicast port
 *      @param {number} config.ttl udp ttl
 * @param {number} id UDP handle, required.
 * @returns Handle pointer
 */
udp.udpCreate = function (config, id) {
	let pointer = udpObj.udpCreate(config);
	if (pointer === undefined || pointer === null) {
		throw new Error("udp.udpCreate: create failed")
	}
	dxCommon.handleId("udp", id, pointer)
}

/**
 * UDP destruction
 * @param {number} id UDP handle, required.
 * @returns true/false
 */
udp.udpDestory = function (id) {
	let pointer = dxCommon.handleId("udp", id)
	return udpObj.udpDestory(pointer);
}

/**
 * UDP connection reopened
 * @param {number} id UDP handle, required.
 * @returns true/false
 */
udp.udpSocketReopen = function (id) {
	let pointer = dxCommon.handleId("udp", id)
	return udpObj.udpSocketReopen(pointer)
}

/**
 * UDP data transmission
 * @param {string} data Data to be sent
 * @param {number} id UDP handle, required.
 * @returns true/false
 */
udp.udpSend = function (data, id) {
	let pointer = dxCommon.handleId("udp", id)
	return udpObj.udpSend(pointer, data);
}

/**
 * UDP callback function registration
 * @param {number} udp Handle, required
 * @returns true/false
 */
udp.udpRegisterConfigCb = function (callback, id) {
	let pointer = dxCommon.handleId("udp", id)
	return udpObj.udpRegisterConfigCb(pointer, callback);
}

/**
 * Configure UDP
 * @param {object} config UDP related parameters, required.
 * 		@param {string} config.group_ip UDP multicast address
 *      @param {number} config.group_port upd multicast port
 *      @param {number} config.ttl udp ttl
 * @param {number} id UDP handle, required.
 * @returns true/false
 */
udp.udpSetConfig = function (config, id) {
	let pointer = dxCommon.handleId("udp", id)
	return udpObj.udpSetConfig(pointer, config);
}

/**
 * Check if the UDP message queue is empty.
 * @returns true/false
 */
udp.msgIsEmpty = function () {
	return udpObj.msgIsEmpty()
}

/**
 * Read data from the UDP message queue.
 * @returns json
 */
udp.msgReceive = function () {
	return udpObj.msgReceive();
}

/**
 * Get UDP queue size
 * @returns num
 */
udp.msgSize = function () {
	return udpObj.msgSize();
}

export default udp;
